package com.example.springboot.service;


import com.example.springboot.config.RabbitConfig;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 测试mq的解释与消费
 * <p>用test用例发送消息，并发送remote mq中
 */
@Component
public class MQConsumer {

    /**
     * RabbitHandler 用于标记处理具体类型消息的方法。一个类中的多个方法重名方法可以使用 @RabbitHandler 来处理不同类型的消息体
     */
    @RabbitHandler
    @RabbitListener(queues = RabbitConfig.PROCESS_QUEUE)
    public void consumer(String msg){
        System.out.println("consumer : " + msg);

    }

    @RabbitHandler
    @RabbitListener(queues = RabbitConfig.PROCESS_QUEUE)
    public void consumer(Message msg){
        System.out.println("consumers : " + msg.toString());

    }

    @RabbitHandler
    @RabbitListener(queues = "local_process_queue1")
    public void consumerAndSend(String msg){
        System.out.println("cas = " + msg);
        remoteRabbitTemplate.convertAndSend(RabbitConfig.REMOTE_PROCESS_QUEUE, LocalDateTime.now().toString() +" : "+msg);
    }

    @Autowired
    private RabbitTemplate remoteRabbitTemplate;


}
